package cn.sxt.logistics.controller;


import cn.sxt.logistics.pojo.BaseData;
import cn.sxt.logistics.pojo.MessageObject;
import cn.sxt.logistics.pojo.Permission;
import cn.sxt.logistics.pojo.ResultObject;
import cn.sxt.logistics.service.IBaseDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 基础数据表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-17
 */
@Controller
@RequestMapping("/baseData")
public class BaseDataController {

    @Autowired
    private IBaseDataService baseDataService;

    @GetMapping("/baseDataManage")
    public String baseDataManage(){
        return "baseDataManage";
    }

    /*基础数据列表*/
    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             String keyword){

        //创建分页对象
        Page<Map<String,Object>> permissionPage = new Page<>(pageNum,pageSize);

        //创建条件对象
        QueryWrapper<Map<String,Object>> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(keyword)){
            //按照子表的权限名称作为条件
            queryWrapper.like("c.base_name",keyword);

            queryWrapper.or();

            //按照父表权限名称作为条件(权限分类搜索)
            queryWrapper.like("p.base_name",keyword);
        }


        //按照类型排序
        queryWrapper.orderByAsc("c.parent_id");

        //执行查询方法
        IPage<Map<String, Object>> result = baseDataService.selectBaseDatas(permissionPage, queryWrapper);


        return new ResultObject(result);
    }


    //删除功能
    @GetMapping("/delete")
    @ResponseBody
    public MessageObject delete(Long baseId){
        /**
         *
         * 判断当前要删除的基础数据是否是类别（并且还有子数据）
         * 是：不能删除
         * 不是：直接删除
         *
         */
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",baseId);
        List<BaseData> children = baseDataService.list(wrapper);


        //有子权限
        if(children.size() > 0){
            return new MessageObject(0,"该基础数据还有子数据，不能删除");
        }

        boolean flag = baseDataService.removeById(baseId);
        MessageObject mo = new MessageObject(1, "删除数据成功");
        if(!flag){
            mo = new MessageObject(0,"删除数据失败，请联系管理员");
        }
        return mo;
    }


    /*跳转到编辑页面*/
    @GetMapping("/baseDataEdit")
    public String permissionEdit(Model m, Long baseId){

        //如果有id说明是修改操作
        if(baseId !=null){
            BaseData baseData = baseDataService.getById(baseId);
            m.addAttribute("baseData",baseData);
        }

        //查询出所有的基础数据类型（parent_id=null）就是基础数据类别
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.isNull("parent_id");
        List<BaseData> parents = baseDataService.list(wrapper);

        m.addAttribute("parents",parents);


        return "baseDataEdit";
    }

    /*新增数据*/
    @PostMapping("/insert")
    @ResponseBody
    public MessageObject insert(BaseData baseData){

        //新增管理员
        boolean flag = baseDataService.saveOrUpdate(baseData);


        MessageObject mo = new MessageObject(1, "新增数据成功");
        if(!flag){
            mo = new MessageObject(0,"新增数据失败，请联系管理员");
        }
        return mo;
    }



    /*修改数据*/
    @PostMapping("/update")
    @ResponseBody
    public MessageObject update(BaseData baseData){
        //新增管理员
        boolean flag = baseDataService.saveOrUpdate(baseData);
        MessageObject mo = new MessageObject(1, "修改数据成功");
        if(!flag){
            mo = new MessageObject(0,"修改数据失败，请联系管理员");
        }
        return mo;
    }

}

